<?php

defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME', microtime(true));
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 0);
defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER', true);
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',true);
defined('YII_PATH') or define('YII_PATH', dirname(__FILE__));

class YiiBase
{
	public static $classMap = array();
	public static $enableIncludePath = true;
	private static $_aliases = array('system'=>YII_PATH);
	private static $_imports=array();
	private static $_includePaths;
	private static $_app;
	private static $_logger;
	
	public static function getVersion()
	{
		return '1.1.10';
	}
	
	public static function createWebApplication($config=null)
	{
		return self::createApplication('CWebApplication',$config);
	}
	
	public static function createApplication($class,$config=null)
	{
		return new $class($config);
	}
	
	public static function autoload($className)
	{
		if(isset(self::$classMap[$className]))
		{
			include(self::$classMap[$className]);
		}
		else if(isset(self::$_coreClass[$className]))
		{
			include(YII_PATH.self::$_coreClasses[$className]);
			
		}
		else 
		{
			if(strpos($className,'\\')===false)
			{
				if(self::$enableIncludePath==false)
				{
					foreach(self::$_includePaths as $path)
					{
						$classFile = $path.DIRECTORY_SEPARATOR.$className.'.php';
						if(is_file($classFile))
						{
							include($classFile);
							break;
						}
					}
				}
				else 
				{
					include($className.'.php');
				}
			}
			else 
			{
				$namespace=str_replace('\\','.', ltrim($className,'\\'));
				if(($path=self::getPathOfAlias($namespace))!==false)
				{
					include($path.'.php');
				}
				else 
				{
					return false;
				}
			}
			return class_exists($className,false) || interface_exists($className,FALSE);
		}
		return true;
		
	}
	
	public static function registerAutoloader($callback,$append=false)
	{
		if($append)
		{
			self::$enableIncludePath=false;
			spl_autoload_register($callback);
		}
		else
		{
			spl_autoload_unregister(array('YiiBase','autolaod'));
			spl_autoload_register($callback);
			spl_autoload_register(array('YiiBase','autoload'));
		}
		
	}
	
	private static $_coreClasses = array(
		'CApplication'=>'/base/CApplication.php',
		'CApplicationComponent'=>'/base/CApplicationComponent.php',
		'CBehavior'=>'/base/CBehavior.php',
		'CComponent'=>'/base/CComponent.php',
		'CErrorEvent'=>'/base/CErrorEvent.php',
		'CErrorHandler'=>'/base/CErrorHandler.php',
		'CException'=>'/base/CException.php',
		'CExceptionEvent'=>'/base/CExceptionEvent.php',
		'CHttpException'=>'/base/CHttpException.php',
		'CModel'=>'/base/CModel.php',
	
	);
	
	public static function setApplication($app)
	{
		if(self::$_app===null || $app===null)
		{
			self::$_app=$app;
		}
		else 
		{
			throw new CException('yii application can only be created once.');
		}
	}
	
}

spl_autoload_register(array('YiiBase','autoload'));
require(YII_PATH.'/base/interfaces.php');

